package com.example.zjx8969.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import androidx.core.content.ContextCompat;
import androidx.appcompat.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;


/**
 * Created by cl2567 on 2018/5/11.
 */

/**
 * 4个验证码输入框
 */
public class VerifyCodeEditText extends AppCompatEditText implements TextWatcher {
    private Paint mCodeTextPaint;
    private Paint mCodeBgPaint;
    private int mBaseLineY;
    private final int verifyCodeLength;
    private String[] inputString;
    private Rect[] locations;
    private int mStrokeWidth;
    private TextWatcher textWatcher;

    public VerifyCodeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        verifyCodeLength = getResources().getInteger(R.integer.verifyCodeLength);
        inputString = new String[verifyCodeLength];
        locations = new Rect[verifyCodeLength];
        initParams();
        initEditTextParams();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        initRect(w, h);
        initTextPaint(h);
    }

    private void initParams() {
        for (int i = locations.length - 1; i >= 0; i--) {
            inputString[i] = "";
            locations[i] = new Rect();
        }
        mStrokeWidth = dip2px(getContext(), 1.5f);

        mCodeBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCodeBgPaint.setStyle(Paint.Style.FILL);
        mCodeBgPaint.setColor(0xffdcdcdc);
        mCodeTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCodeTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mCodeTextPaint.setStrokeWidth(mStrokeWidth);
        mCodeTextPaint.setTextAlign(Paint.Align.CENTER);
        mCodeTextPaint.setColor(ContextCompat.getColor(getContext(), android.R.color.black));
    }

    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5F);
    }

    private void initEditTextParams() {
        addTextChangedListener(this);
        setMaxLines(1);
    }

    public void setTextChangeListener(TextWatcher textWatcher) {
        this.textWatcher = textWatcher;
    }

    private void initRect(int w, int h) {
        int padding = (w - verifyCodeLength * h) / (verifyCodeLength - 1);
        for (int i = 0; i < locations.length; i++) {
            Rect rectF = locations[i];
            rectF.left = i * (h + padding);
            rectF.right = h * (i + 1) + i * padding;
            rectF.top = 0;
            rectF.bottom = h;
        }
    }


    private void initTextPaint(int h) {
        float textSize = 0.8f * h;

        mCodeTextPaint.setTextSize(textSize);
        Paint.FontMetricsInt topFm = mCodeTextPaint.getFontMetricsInt();
        mBaseLineY = h / 2 + (topFm.bottom - topFm.top) / 2 - topFm
                .bottom;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        for (int i = locations.length - 1; i >= 0; i--) {
            Rect current = locations[i];
            canvas.drawRect(current.left, current.bottom - mStrokeWidth, current.right, current.bottom, mCodeBgPaint);

            canvas.drawText(inputString[i], (current.left + current.right) >> 1, mBaseLineY, mCodeTextPaint);
        }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        String content = s.toString();
        int length = inputString.length;
        for (int i = length - 1; i >= 0; i--) {
            inputString[i] = content.length() - 1 >= i ? content.substring(i, i + 1) : "";
        }


        invalidate();
        if (textWatcher != null) {
            textWatcher.afterTextChanged(s);
        }
    }

    public String getRealCode() {
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : inputString) {
            stringBuilder.append(s);
        }
        return stringBuilder.toString();
    }

    public boolean isValidCode() {
        for (String s : inputString) {
            if (TextUtils.isEmpty(s)) return false;
        }
        return true;
    }
}
